package com.goldgov.noticetemplate.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.gtiles.components.securityworkbench.bean.SwbAuthUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.goldgov.kcloud.core.json.JsonObject;
import com.goldgov.kcloud.core.json.JsonQueryObject;
import com.goldgov.kcloud.core.json.JsonSuccessObject;
import com.goldgov.login.web.model.LoginAuthUser;
import com.goldgov.notice.service.Notice;
import com.goldgov.noticetemplate.service.NoticeTemplate;
import com.goldgov.noticetemplate.service.NoticeTemplateQuery;
import com.goldgov.noticetemplate.service.NoticeTemplateService;
import com.goldgov.utils.JsonUtil;
import com.goldgov.utils.PropertyUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

/** 
 * @ClassName: NoticeTemplateController 
 * @Description: TODO
 * @author hanxiangyu
 * @date 2018年9月1日 下午7:19:16 
 */

@RestController
@RequestMapping("/workbench/noticetemplate")
@Api("通知模板接口")
public class NoticeTemplateController {
	@Autowired
	private NoticeTemplateService noticeTemplateService;
	
	
	@PostMapping
	@ApiOperation(value = "新增通知模板", notes ="")
	public JsonObject<Object> addNoticeTemplate(NoticeTemplate obj,HttpServletRequest request,HttpServletResponse response){
		SwbAuthUser user=LoginAuthUser.getUserByAdmin(request);
		obj.setCreatorID(user.getEntityID());
		obj.setCreatorName(user.getName());
		obj.setCreateDate(new Date());
		obj.setLastUpdateDate(new Date());
		obj.setActiveState(NoticeTemplateQuery.ACTIVE_NO);
		if (!PropertyUtil.objectNotEmpty(obj.getState())) {
			obj.setState(NoticeTemplateQuery.STATE_YES);
		}
		if (PropertyUtil.objectNotEmpty(obj.getSenderCode())) {
			obj.setSenderCode(NoticeTemplateQuery.SENDERCODE_NOTIFY);
		}
		if (noticeTemplateService.addTemplate(obj)) {
			if (PropertyUtil.objectNotEmpty(obj.getTemplateID())) {
				if (obj.getState().intValue() == NoticeTemplateQuery.STATE_YES) {
					noticeTemplateService.updateTemplateNoState(obj);
				}
			}
			return JsonUtil.success("新增成功!");
		}else {
			return JsonUtil.error("新增失败!");
		}
	}
	@PostMapping("/delete")
	@ApiOperation(value = "删除通知公告模板", notes = "")
	public JsonObject<Object> deleteTemplate(@ApiParam(value = "templateID", required = true) @RequestParam("ids") String[] ids,HttpServletRequest request,HttpServletResponse response) throws Exception {
		if (PropertyUtil.objectNotEmpty(ids)) {
			try {
				int count = noticeTemplateService.deleteTemplate(ids);
				int deleteCount = ids.length - count;
				if (deleteCount == 0) {
					return JsonUtil.success("删除成功!");
				}else if(deleteCount > 0) {
					if (count > 0) {
						return JsonUtil.success("删除成功!已删除"+count+"个停用的模板");
					}else {
						return JsonUtil.success("请选择未启用的模板!");
					}
				}else {
					return JsonUtil.error("系统出错!请联系管理员");
				}
			} catch (Exception e) {
				return JsonUtil.error("删除失败");
			}
		}else {
			return JsonUtil.error("通知公告模板主键为空!请联系管理员");
		}
	}
	
	@PutMapping
	@ApiOperation(value = "修改通知公告模板", notes = "")
	public JsonObject<Object> updateTemplate(NoticeTemplate obj,HttpServletRequest request,HttpServletResponse response){
		SwbAuthUser user=LoginAuthUser.getUserByAdmin(request);
		obj.setModifierID(user.getEntityID());
		obj.setModifierName(user.getName());
		obj.setLastUpdateDate(new Date());
		if (noticeTemplateService.updateTemplate(obj)) {
			if (PropertyUtil.objectNotEmpty(obj.getTemplateID())) {
				if (obj.getState().intValue() == NoticeTemplateQuery.STATE_YES) {
					noticeTemplateService.updateTemplateNoState(obj);
				}
			}
			return JsonUtil.success("修改成功!");
		}else {
			return JsonUtil.error("修改失败!");
		}
	}
	
	@GetMapping
	@ApiOperation(value = "通知公告模板列表", notes = "")
	public JsonQueryObject<NoticeTemplate> findNoticeTemplateList(@ApiIgnore NoticeTemplateQuery query,HttpServletRequest request,HttpServletResponse response){
		List<NoticeTemplate> list = noticeTemplateService.findTemplateList(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	
	
	@GetMapping("/{id}")
	@ApiOperation(value = "根据主键查询会议记录", notes ="工作日志详情")
	public JsonSuccessObject<NoticeTemplate> getConference(@ApiParam(value="templateID",required=false) @PathVariable("id") String id,HttpServletRequest request){
		NoticeTemplate template = noticeTemplateService.findNoticeTemplateById(id);
		JsonSuccessObject<NoticeTemplate> returnResult = new JsonSuccessObject<>(template);
		return returnResult;
	}
	
	@PostMapping("/enableOrDisable")
	public JsonObject<Object> enableOrDisable(@ApiParam(value = "templateID", required = true) @RequestParam("ids") String[] ids,@RequestParam("state") Integer state,HttpServletRequest request,HttpServletResponse response){
		if (noticeTemplateService.enableOrDisable(ids, state)) {
			if (state.intValue() == NoticeTemplateQuery.STATE_YES) {
				for (String id : ids) {
					NoticeTemplate template = noticeTemplateService.findNoticeTemplateById(id);
					if (template.getState().intValue() == NoticeTemplateQuery.STATE_YES) {
						noticeTemplateService.updateTemplateNoState(template);
					}
				}
				return JsonUtil.success("启用成功!");
			}else {
				return JsonUtil.success("停用成功!");
			}
		}else {
			if (state.intValue() == NoticeTemplateQuery.STATE_YES) {
				return JsonUtil.error("启用失败!");
			}else {
				return JsonUtil.error("停用失败!");
			}
		}
	}
	
	
}
